// 创建公动画
var animation = wx.createAnimation({
  duration: 40, //动画持续时间
  timingFunction: 'linear'
})
Page({
  open: true, //背景是否不透明
  time: 238, //初始定时器时间
  setInter: '', //存储计时器
  speed: 1, //速度切换，对应time的值
  data: {
    bgColor: "#ffffff", //背景色
    color: ['white', 'red', 'blue', 'yellow', 'pink', 'aqua', 'green', 'skyblue', 'hotpink', ],
    showPageContainer: true,
  },
  onLoad: function () {
    wx.getScreenBrightness({
      success: res => {
        this.setData({
          screenBrightness: res.value * 100
        })
      }
    })
    wx.setKeepScreenOn({
      keepScreenOn: true,
    })
    this.startSetInter();
  },
  // 调整屏幕亮度
  setScreenBrightness(e) {
    wx.setScreenBrightness({
      value: e.detail.value / 100
    })
  },
  // 开始计时器
  startSetInter: function () {
    console.log("开始计时器")
    this.setInter = setInterval(
      () => {
        if (this.open) {
          animation.opacity(0).step();
          this.setData({
            bgColorChange: animation.export(),
          })
          this.open = false
        } else {
          animation.opacity(1).step();
          this.setData({
            bgColorChange: animation.export(),
          })
          this.open = true
        }
      }, this.time)
  },

  //清除计时器
  endSetInter: function () {
    clearInterval(this.setInter)
    animation.opacity(1).step();
    this.setData({
      bgColorChange: animation.export(),
    })
  },
  // 页面卸载时触发
  onUnload: function () {
    this.endSetInter()
  },
  // 点击页面
  clickPage() {
    this.setData({
      showPageContainer: true,
    })
  },
  // 点击遮罩层
  clickoverlay() {
    this.setData({
      showPageContainer: false
    })
  },
  // 改变颜色
  changeColor(e) {
    let {
      index
    } = e.currentTarget.dataset
    this.setData({
      bgColor: this.data.color[index]
    })
  },
  // 改变速度
  changeSpeed(e) {
    let {
      value
    } = e.detail
    console.log(value)
    this.time = 350 - 28 * value
    this.endSetInter()
    this.startSetInter();
  },
  onShareAppMessage() {
    return {
      title: "黑夜里你是最靓的仔",
      path: "/pages/happyTools/pages/handLamp/index",
      imageUrl: '/images/function_icon/yedianLightIcon.png',
    }
  },
  onShareTimeline() {
    return {
      title: '黑夜里你是最靓的仔',
      imageUrl: '/images/function_icon/yedianLightIcon.png',
    }
  },
})